package com.toptech.zsr.my.recyclerview;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.toptech.downloadmanager.FileInfo;
import com.toptech.downloadmanager.ToolUtils.ContantsUtils;
import com.toptech.zsr.my.R;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/3/4.
 */

public class MyRecyclerviewAdapter extends RecyclerView.Adapter<MyRecyclerviewAdapter.MyViewHolder>{
    private static final String TAG = "zsr";

    private List<FileInfo> mDataList;
    private Context mContext;

    public MyRecyclerviewAdapter(Context context, List<FileInfo> mFileList) {
        this.mContext = context;
        this.mDataList = mFileList;
    }

    public interface OnClickItemListener{
        public void setOnClickItemListener(View v, int position);
        public void setonLongClickItemListener(View v, int position);
    }

    private OnClickItemListener mOnClickItemListener;

    public void setOnclickItemListener(OnClickItemListener listener){
        this.mOnClickItemListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).
                inflate(R.layout.progress,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.root_item.setFocusable(true);
        holder.root_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickItemListener.setOnClickItemListener(v,position);
            }
        });

        holder.root_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mOnClickItemListener.setonLongClickItemListener(v,position);
                return true;
            }
        });

        holder.filename.setText(mDataList.get(position).getFileName());
        holder.filestatus.setText("");
        int progress = mDataList.get(position).getProgress();
        holder.filebutton.setText("未开始");
        if (progress > 0){
            if (progress == 100){
                holder.filestatus.setText("已完成");
                holder.filebutton.setText("已完成");
                holder.fileprogressbar.setProgress(progress);
                holder.fileprogresstext.setText("");
            }else{
                holder.fileprogresstext.setText(progress+"%");
                holder.fileprogressbar.setProgress(progress);
                String filedownsize = mDataList.get(position).getFiledownsize();
                String filesize = mDataList.get(position).getFilesize();
                holder.filesizeshow.setText(filedownsize+"MB"+" / "+
                        filesize+"MB");
            }

        }
        if(mDataList.get(position).getPriority() != ContantsUtils.PRIORITY_HIGHTEST){
            holder.file_icon_special.setVisibility(View.INVISIBLE);
        }



        //设置tag
        holder.fileprogressbar.setTag(mDataList.get(position).getApkurl()+ ContantsUtils.TAG_PROGRESSBAR);
        holder.fileprogresstext.setTag(mDataList.get(position).getApkurl()+ ContantsUtils.TAG_TEXTPROGRESS);
        holder.filestatus.setTag(mDataList.get(position).getApkurl()+ContantsUtils.TAG_FILESTATUS);
        holder.filesizeshow.setTag(mDataList.get(position).getApkurl()+ContantsUtils.TAG_FILESIZE );
        holder.filebutton.setTag(mDataList.get(position).getApkurl()+ContantsUtils.TAG_FILEBTN);
        holder.file_icon_special.setTag(mDataList.get(position).getApkurl()+ContantsUtils.TAG_SPECIAL);
        holder.filebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickItemListener.setOnClickItemListener(v,position);
            }
        });


    }

    @Override
    public int getItemCount() {

        return mDataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView filename,filestatus,fileprogresstext,filesizeshow;
        Button filebutton;
        ProgressBar fileprogressbar;
        CardView root_item;
        private ImageView file_icon_special;
        public MyViewHolder(View itemView) {
            super(itemView);
            filename = (TextView) itemView.findViewById(R.id.file_name);
            filestatus = (TextView) itemView.findViewById(R.id.file_status);
            fileprogresstext = (TextView) itemView.findViewById(R.id.file_progress_text);
            filesizeshow = (TextView) itemView.findViewById(R.id.file_size_show);
            filebutton = (Button) itemView.findViewById(R.id.file_pause);
            fileprogressbar = (ProgressBar) itemView.findViewById(R.id.file_progress);
            root_item = (CardView) itemView.findViewById(R.id.root_cardview);
            file_icon_special = (ImageView) itemView.findViewById(R.id.file_icon_special);
        }
    }


    private String getFloatToString(Float value){
        DecimalFormat decimalFormat =
                new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        return  decimalFormat.format(value);
    }
}
